package com.edu.app.controller.linkcontrollers;

import com.edu.app.bean.core.SysCustomer;
import com.edu.app.bean.linkbeans.SysCustomerLesson;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.model.CustomerLessonModel;
import com.edu.app.service.sys.linkservices.SysCustomerLessonService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/customer_lesson")
@RestController
public class SysCustomerLessonController {

    @Autowired
    private SysCustomerLessonService sysCustomerLessonService;

    @GetMapping(value = "")
    @RequiresAuthentication
    public ModelMap getCustomerLessonList(){
        List<SysCustomerLesson> sysCustomerLessonList = sysCustomerLessonService.getObjectList();
        return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_LESSON_SUCCEED, StaticConstant.CUSTOMER_LESSON_LIST, sysCustomerLessonList);
    }

    @GetMapping(value = "/{id}")
    @RequiresAuthentication
    public ModelMap getCustomerLessonById(@PathVariable Integer id){
        SysCustomerLesson sysCustomerLesson = sysCustomerLessonService.getObjectById(id);
        return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_LESSON_SUCCEED, StaticConstant.CUSTOMER_LESSON, sysCustomerLesson);
    }

    /*@GetMapping(value = "/getLessonByCustomerId")
    @RequiresAuthentication
    public ModelMap getLessonByCustomerId(@RequestParam Integer id){
        List<NameValueItems> nameValueItemsList = new LinkedList<NameValueItems>();

        List<SysListOfValue> sysLessonList = sysCustomerLessonService.getLessonByCustomerId(id);
        for(SysListOfValue sysLesson: sysLessonList){
            SysCustomerLesson sysCustomerLesson = sysCustomerLessonService.getCustomerLesson(id, sysLesson.getId());
            NameValueItems nameValueItems = new NameValueItems();
            nameValueItems.setId(sysCustomerLesson.getId());
            nameValueItems.setName(sysLesson.getValue());
            nameValueItems.setValue(sysCustomerLesson.getPoints());
            nameValueItemsList.add(nameValueItems);
        }
        return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_LESSON_SUCCEED, StaticConstant.CUSTOMER_LESSON_LIST, nameValueItemsList);
    }*/

    @GetMapping(value = "getCustomerLessonModelByCusId")
    @RequiresAuthentication
    public ModelMap getCustomerLessonModelByCusId(@RequestParam Integer id){
        List<CustomerLessonModel> customerLessonModelList = sysCustomerLessonService.getCustomerLessonModelByCusId(id);
        return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_LESSON_MODEL_SUCCEED, StaticConstant.CUSTOMER_LESSON_MODEL_LIST, customerLessonModelList);
    }


    @GetMapping(value = "getCustomerByLessonId")
    @RequiresAuthentication
    public ModelMap getCustomerByLessonId(@RequestParam Integer id){
        List<SysCustomer> sysCustomerList = sysCustomerLessonService.getCustomerByLessonId(id);
        return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_SUCCEED, StaticConstant.CUSTOMER_LIST, sysCustomerList);
    }

    @GetMapping(value = "/customer_lesson")
    @RequiresAuthentication
    public ModelMap getCustomerLessonById(@RequestParam Integer customer_id, @RequestParam Integer lesson_id){
        SysCustomerLesson sysCustomerLesson = sysCustomerLessonService.getCustomerLesson(customer_id, lesson_id);
        return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_LESSON_SUCCEED, StaticConstant.CUSTOMER_LESSON, sysCustomerLesson);
    }

    @PostMapping(value = "")
    @RequiresAuthentication
    public ModelMap addCustomerLesson(@RequestHeader("Authorization") String token, @RequestBody SysCustomerLesson sysCustomerLesson){

        try{
            sysCustomerLesson.initializeSystemColumns(JWTUtil.getUsername(token));
            Integer r = sysCustomerLessonService.add(sysCustomerLesson);
            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_CUSTOMER_LESSON_SUCCEED, null ,null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.CUSTOMER_LESSON_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @PutMapping(value = "")
    @RequiresAuthentication
    public ModelMap updateCustomerLesson(@RequestHeader("Authorization") String token, @RequestBody SysCustomerLesson sysCustomerLesson){

        try{
            sysCustomerLesson.initializeSystemColumns(JWTUtil.getUsername(token));
            Integer r = sysCustomerLessonService.update(sysCustomerLesson);
            if(r > 0){
                return MsgUtil.retMsg(SysMessage.UPDATE_CUSTOMER_LESSON_SUCCEED, null ,null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @DeleteMapping(value = "")
    @RequiresAuthentication
    public ModelMap deleteCustomerLesson(@RequestParam Integer id){

        try{
            Integer r = sysCustomerLessonService.delete(id);
            if(r > 0){
                return MsgUtil.retMsg(SysMessage.DELETE_CUSTOMER_LESSON_SUCCEED, null ,null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }
}
